package com.terabits.smartriver.util;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.terabits.smartriver.service.impl.BaseService.oToD;

/**
 * Created by Yin on 2018/12/20
 */
public class DateUtils {

    public static String DateToString(Date date, String pattern) {
        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date StringToDate(String dateStr, String pattern) {

        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }

        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取n小时前的时间
     *
     * @return
     */
    public static String getBeforeTime(Integer date) {
        String returnstr = "";
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - date);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        returnstr = df.format(calendar.getTime());
        return returnstr;
    }

    public static String getTime(Integer date) {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, date);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String result = sm.format(calendar.getTime());
        return result;
    }

//    public static void main(String[] args) {
//        System.out.println(getTime(-1));
//    }
//    public static void main(String[] args) {
//        SimpleDateFormat localTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String a="2019-07-26 01:42:04";
//        String b="2019-07-25 19:10:42";
//        try {
//            Date date = localTime.parse(String.valueOf(a));
//            Date date1 = localTime.parse(String.valueOf(b));
//            System.out.println((date.getTime() - date1.getTime()) / 1000  / 3600);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        BigDecimal a;
//        Double v = 0.245;
//        Double c = 0.64523;
//        BigDecimal bd1 = new BigDecimal(Double.toString(v));
//        BigDecimal bd2 = new BigDecimal(Double.toString(c));
//        double v1 = bd2.subtract(bd1).doubleValue();
//        if (v1 == 0.0) {
//            System.out.println(v1);
//        }
//        System.out.println(v1);
//        System.out.println(String.valueOf(a).substring(0,4));
//    }

    public static String getDate(Integer month, Integer dayOfmonth, Integer date) {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (month != null) {
            calendar.add(Calendar.MONTH, month);
        }
        if (dayOfmonth != null) {
            calendar.set(Calendar.DAY_OF_MONTH, dayOfmonth);
        }
        if (date != null) {
            calendar.add(Calendar.DATE, date);
        }

        String result = sm.format(calendar.getTime());
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getDate(null, null, -1));
    }
    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date currentTime() {
        TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(time);

        return new Date();

    }

    /**
     * 过去n天
     *
     * @return
     */
    public static String getTheDayBeforeYesterdayTimeFormat(Integer date) {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前日期
        Calendar firstDayInAMonth = Calendar.getInstance();
        firstDayInAMonth.add(Calendar.DATE, date);
        String result = sm.format(firstDayInAMonth.getTime());
        return result;
    }

    public static String getDateOnlyYMD(Integer month, Integer dayOfmonth, Integer date) {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (month != null) {
            calendar.add(Calendar.MONTH, month);
        }
        if (dayOfmonth != null) {
            calendar.set(Calendar.DAY_OF_MONTH, dayOfmonth);
        }
        if (date != null) {
            calendar.add(Calendar.DATE, date);
        }

        String result = sm.format(calendar.getTime());
        return result;
    }
}
